﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IP.Core.IPCommon;
using IP.Core.IPData;
using IP.Core.IPUserService;

using BKIDS;
using BKIDS.CDBNames;
using BKIUS;
using System.Data;

public partial class QuanLyNhanSu_F207_DanhSachNhanSu : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            load_2_cbo_loaihocvien();
            load_data_2_cbo_chucvu();
            XoaTrang();
        }
    }

    #region Members

    #endregion

    #region Public Interfaces
    public string mapping_gender(string ip_str_gen)
    {
        if (ip_str_gen.Equals("Y"))
            return "Nam";
        else if (ip_str_gen.Equals("All")) return "Tất cả";
        return "Nữ";
    }

    public string mapping_thanh_vien(decimal ip_id_dt)
    {
        US_DM_THANH_VIEN v_us_thanh_vien = new US_DM_THANH_VIEN(ip_id_dt);
        if (v_us_thanh_vien== null)
            return "Không";
        else
        {
            US_CM_DM_TU_DIEN v_cm_dm_tu_dien = new US_CM_DM_TU_DIEN(v_us_thanh_vien.dcID_CHUC_VU);
            if (v_cm_dm_tu_dien != null)
                return v_cm_dm_tu_dien.strTEN_NGAN.ToString();
            else
                return "Không";
        }
    }

    public string mapping_giang_vien(decimal ip_id_dt)
    {
        US_DM_GIANG_VIEN v_us_giang_vien = new US_DM_GIANG_VIEN(ip_id_dt);
        if (v_us_giang_vien == null)
            return "Không";
        else
        {
            return "Có";
        }
    }

    public string mapping_hoc_vien(decimal ip_id_dt)
    {
        US_DM_HOC_VIEN v_us_hoc_vien = new US_DM_HOC_VIEN(ip_id_dt);
        if (v_us_hoc_vien == null)
            return "Không";
        else
        {
            US_DM_LOAI_HOC_VIEN v_dm_loai_hoc_vien = new US_DM_LOAI_HOC_VIEN(v_us_hoc_vien.dcID_LOAI_HV);
            if (v_dm_loai_hoc_vien != null)
                return v_dm_loai_hoc_vien.strTEN_LOAI_HV.ToString();
            else
                return "Không";
        }
    }

    public string mapping_email_bkindex(decimal ip_id_dt)
    {
        US_DM_THANH_VIEN v_us_thanh_vien = new US_DM_THANH_VIEN(ip_id_dt);
        if (v_us_thanh_vien != null)
            return v_us_thanh_vien.strEMAIL_BKINDEX;
        else
        {
            US_DM_GIANG_VIEN v_us_giang_vien = new US_DM_GIANG_VIEN(ip_id_dt);
            if (v_us_giang_vien != null)
                return v_us_giang_vien.strEMAIL_BKINDEX;
            else
                return "";
        }
    }
    #endregion

    #region Private Methods
    private void Search_data_doi_tuong(string ip_str_hoten, decimal ip_dc_thanhsinh, string ip_str_cuongvi
                                            , string ip_str_gioitinh, string ip_str_ngang, string ip_str_truong
                                            , decimal ip_dc_id_chucvu, decimal ip_dc_id_loaihv, DS_DM_DOI_TUONG ip_ds_dm_doi_tuong)
    {


        US_DM_DOI_TUONG v_us_dm_doi_tuong = new US_DM_DOI_TUONG();
        try
        {
            ip_ds_dm_doi_tuong.Clear();
            // Đổ dữ liệu từ US vào DS            
            if (ip_str_cuongvi.Equals("ALL"))
                v_us_dm_doi_tuong.search_doi_tuong(ip_str_hoten, ip_dc_thanhsinh, ip_str_gioitinh, ip_str_ngang, ip_str_truong ,ip_ds_dm_doi_tuong);
            else
            {
                if (ip_str_cuongvi.Equals("THANH_VIEN"))
                    v_us_dm_doi_tuong.search_doi_tuong_thanh_vien(ip_str_hoten, ip_dc_thanhsinh, ip_str_gioitinh, ip_str_ngang, ip_str_truong,ip_dc_id_chucvu, ip_ds_dm_doi_tuong);
                if (ip_str_cuongvi.Equals("HOC_VIEN"))
                    v_us_dm_doi_tuong.search_doi_tuong_hoc_vien(ip_str_hoten, ip_dc_thanhsinh, ip_str_gioitinh, ip_str_ngang, ip_str_truong,ip_dc_id_loaihv, ip_ds_dm_doi_tuong);
                if (ip_str_cuongvi.Equals("GIANG_VIEN"))
                    v_us_dm_doi_tuong.search_doi_tuong_giang_vien(ip_str_hoten, ip_dc_thanhsinh, ip_str_gioitinh, ip_str_ngang, ip_str_truong, ip_ds_dm_doi_tuong);
            }            
        }
        catch (Exception v_e)
        {
            //nhớ using Ip.Common
            m_lbl_thong_bao.Text = v_e.Message;
            m_lbl_thong_bao.Visible = true;

        }

    }

    private string get_gender_search()
    {
        string v_str_gen = "";
        if (rdl_gender_check.Items[0].Selected) v_str_gen = "A";
        else if (rdl_gender_check.Items[1].Selected) v_str_gen = "Y";
        else v_str_gen = "N";
        return v_str_gen;
    }
    private void load_data_2_grv_doi_tuong()
    {
        try
        {
            DS_DM_DOI_TUONG v_ds_dm_doi_tuong = new DS_DM_DOI_TUONG();
            Search_data_doi_tuong(CIPConvert.ToStr(m_txt_ho_ten.Text), CIPConvert.ToDecimal(m_cbo_thang_sinh.SelectedValue)
                                        , CIPConvert.ToStr(m_cbo_cuong_vi.SelectedValue), CIPConvert.ToStr(get_gender_search())
                                        , CIPConvert.ToStr(m_txt_Khoanganh.Text), CIPConvert.ToStr(m_txt_truong.Text)
                                        , CIPConvert.ToDecimal(m_cbo_chuc_vu.SelectedValue), CIPConvert.ToDecimal(m_cbo_loai_hoc_vien.SelectedValue),v_ds_dm_doi_tuong);            
            // Nếu dữ liệu trống
            if (v_ds_dm_doi_tuong.DM_DOI_TUONG.Rows.Count == 0)
            {
                m_lbl_thong_bao.Text = "không có kết quả nào";
            }

            m_lbl_soketqua.Text = v_ds_dm_doi_tuong.DM_DOI_TUONG.Rows.Count.ToString();
            m_grv_dm_doi_tuong.DataSource = v_ds_dm_doi_tuong.DM_DOI_TUONG;
            m_grv_dm_doi_tuong.DataBind();
        }
        catch (Exception v_e)
        {
            //nhớ using Ip.Common
            m_lbl_thong_bao.Text = v_e.Message;
            m_lbl_thong_bao.Visible = true;

        }

    }

    private void load_data_2_cbo_chucvu()
    {
        DS_CM_DM_TU_DIEN m_ds_cm_dm_tudien = new DS_CM_DM_TU_DIEN();
        US_CM_DM_TU_DIEN m_us_cm_dm_tudien = new US_CM_DM_TU_DIEN();
        try
        {
            m_us_cm_dm_tudien.FillDataset(m_ds_cm_dm_tudien);
            if (m_ds_cm_dm_tudien.CM_DM_TU_DIEN != null && m_ds_cm_dm_tudien.CM_DM_TU_DIEN.Rows.Count > 0)
            {
                m_cbo_chuc_vu.Items.Add(new ListItem("Tất cả", "0"));
                for (int v_i = 0; v_i < m_ds_cm_dm_tudien.CM_DM_TU_DIEN.Rows.Count; v_i++)
                {
                    m_cbo_chuc_vu.Items.Add(new ListItem(m_ds_cm_dm_tudien.CM_DM_TU_DIEN.Rows[v_i]["TEN_NGAN"].ToString(),m_ds_cm_dm_tudien.CM_DM_TU_DIEN.Rows[v_i]["ID"].ToString()));
                }
            }
        }
        catch (Exception v_e)
        {
            m_lbl_thong_bao.Text = v_e.Message;
            m_lbl_thong_bao.Visible = true;
        }
    }

    private void load_2_cbo_loaihocvien()
    {
        try
        {
            US_DM_LOAI_HOC_VIEN v_us_loaihocvien = new US_DM_LOAI_HOC_VIEN();
            DS_DM_LOAI_HOC_VIEN v_ds_loaihocvien = new DS_DM_LOAI_HOC_VIEN();
            v_us_loaihocvien.FillDataset(v_ds_loaihocvien);
            m_cbo_loai_hoc_vien.Items.Add(new ListItem("Tất cả", "0"));
            if (v_ds_loaihocvien.DM_LOAI_HOC_VIEN != null && v_ds_loaihocvien.DM_LOAI_HOC_VIEN.Rows.Count > 0)
            {                
                for (int v_i = 0; v_i < v_ds_loaihocvien.DM_LOAI_HOC_VIEN.Rows.Count; v_i++)
                {
                    m_cbo_loai_hoc_vien.Items.Add(new ListItem(v_ds_loaihocvien.DM_LOAI_HOC_VIEN.Rows[v_i]["TEN_LOAI_HV"].ToString(), v_ds_loaihocvien.DM_LOAI_HOC_VIEN.Rows[v_i]["ID"].ToString()));
                }
            }
        }
        catch (Exception v_e)
        {
            m_lbl_lois.Text = v_e.Message;
            m_lbl_lois.Visible = true;
        }

    }

    private void XoaTrang()
    {
        m_txt_ho_ten.Text = "";
        m_txt_Khoanganh.Text = "";
        m_txt_truong.Text = "";
        m_cbo_thang_sinh.SelectedValue = "0";
        m_cbo_loai_hoc_vien.SelectedValue = "0";
        m_cbo_cuong_vi.SelectedValue = "ALL";
        m_cbo_chuc_vu.SelectedValue = "0";
        rdl_gender_check.SelectedValue = "ALL";
        m_lbl_lois.Visible = false;
        m_lbl_thong_bao.Visible = false;
        m_btt_xuatexcel.Enabled = false;
        
    }

    private void loadDSExprort(ref string strTable)
    {
        int v_i_so_thu_tu = 0;
        DS_DM_DOI_TUONG v_ds_dm_doi_tuong = new DS_DM_DOI_TUONG();
        Search_data_doi_tuong(CIPConvert.ToStr(m_txt_ho_ten.Text), CIPConvert.ToDecimal(m_cbo_thang_sinh.SelectedValue)
                                    , CIPConvert.ToStr(m_cbo_cuong_vi.SelectedValue), CIPConvert.ToStr(get_gender_search())
                                    , CIPConvert.ToStr(m_txt_Khoanganh.Text), CIPConvert.ToStr(m_txt_truong.Text)
                                    , CIPConvert.ToDecimal(m_cbo_chuc_vu.SelectedValue), CIPConvert.ToDecimal(m_cbo_loai_hoc_vien.SelectedValue), v_ds_dm_doi_tuong);
            
        // Mỗi cột dữ liệu ứng với từng dòng là label
        foreach (DataRow grv in v_ds_dm_doi_tuong.DM_DOI_TUONG.Rows)
        {
            strTable += "\n<tr>";
            strTable += "\n<td style='width:12%;' class='cssTitleReport' nowrap='nowrap'>" + v_i_so_thu_tu + "</td>";
            strTable += "\n<td style='width:12%;' class='cssTitleReport' nowrap='nowrap'>" + (grv[DM_DOI_TUONG.MA_DOI_TUONG]).ToString() + "</td>";
            strTable += "\n<td style='width:12%;' class='cssTitleReport' nowrap='nowrap'>" + (grv[DM_DOI_TUONG.HO_VA_TEN_DEM]).ToString().Trim() + " " + (grv[DM_DOI_TUONG.TEN]).ToString() + "</td>";
            strTable += "\n<td style='width:12%;' class='cssTitleReport' nowrap='nowrap'>" + string.Format("{0:dd/MM/yyy}", grv[DM_DOI_TUONG.NGAY_SINH]) + "</td>";
            strTable += "\n<td style='width:12%;' class='cssTitleReport' nowrap='nowrap'>" + mapping_gender(CIPConvert.ToStr(grv[DM_DOI_TUONG.GIOI_TINH_YN])) + "</td>";
            strTable += "\n<td style='width:12%;' class='cssTitleReport' nowrap='nowrap'>" + mapping_thanh_vien(CIPConvert.ToDecimal(grv[DM_DOI_TUONG.ID])) + "</td>";
            strTable += "\n<td style='width:12%;' class='cssTitleReport' nowrap='nowrap'>" + mapping_giang_vien(CIPConvert.ToDecimal(grv[DM_DOI_TUONG.ID])) + "</td>";
            strTable += "\n<td style='width:12%;' class='cssTitleReport' nowrap='nowrap'>" + mapping_hoc_vien(CIPConvert.ToDecimal(grv[DM_DOI_TUONG.ID])) + "</td>";
            strTable += "\n<td style='width:12%;' class='cssTitleReport' nowrap='nowrap'>" +  (grv[DM_DOI_TUONG.QUE_QUAN]).ToString() + "</td>";
            strTable += "\n<td style='width:12%;' class='cssTitleReport' nowrap='nowrap'>" +  (grv[DM_DOI_TUONG.DIA_CHI_HIEN_TAI]).ToString() + "</td>";
            strTable += "\n<td style='width:12%;' class='cssTitleReport' nowrap='nowrap'>" + "'" + (grv[DM_DOI_TUONG.DIEN_THOAI]).ToString() + "</td>";
            strTable += "\n<td style='width:12%;' class='cssTitleReport' nowrap='nowrap'>" + "'" + (grv[DM_DOI_TUONG.SO_CMND]).ToString() + "</td>";
            strTable += "\n<td style='width:12%;' class='cssTitleReport' nowrap='nowrap'>" + string.Format("{0:dd/MM/yyyy}",grv[DM_DOI_TUONG.NGAY_CAP]) + "</td>";
            strTable += "\n<td style='width:12%;' class='cssTitleReport' nowrap='nowrap'>" + (grv[DM_DOI_TUONG.NOI_CAP]).ToString() + "</td>";
            strTable += "\n<td style='width:12%;' class='cssTitleReport' nowrap='nowrap'>" + (grv[DM_DOI_TUONG.EMAIL_CA_NHAN]).ToString() + "</td>";
            strTable += "\n<td style='width:12%;' class='cssTitleReport' nowrap='nowrap'>" + mapping_email_bkindex(CIPConvert.ToDecimal(grv[DM_DOI_TUONG.ID])) + "</td>";
            strTable += "\n<td style='width:12%;' class='cssTitleReport' nowrap='nowrap'>" + "'" + (grv[DM_DOI_TUONG.SO_TAI_KHOAN]).ToString() + "</td>";
            strTable += "\n<td style='width:12%;' class='cssTitleReport' nowrap='nowrap'>" + (grv[DM_DOI_TUONG.TEN_NGAN_HANG]).ToString() + "</td>";
            strTable += "\n<td style='width:12%;' class='cssTitleReport' nowrap='nowrap'>" + (grv[DM_DOI_TUONG.KHOA_NGANH]).ToString() + "</td>";            
            strTable += "\n<td style='width:12%;' class='cssTitleReport' nowrap='nowrap'>" + (grv[DM_DOI_TUONG.TRUONG_DAO_TAO]).ToString() + "</td>";
            strTable += "\n<td style='width:12%;' class='cssTitleReport' nowrap='nowrap'>" + (grv[DM_DOI_TUONG.GIOI_THIEU]).ToString() + "</td>";            
            strTable += "\n</tr>";
        }
    }

    private void loadTieuDe(ref string strTable)
    {
        strTable += "<table cellpadding='2' cellspacing='0' class='cssTableReport'>";
        strTable += "\n<tr>";
        strTable += "\n<td><align='center' class='cssTableView' style='width:100%;' nowrap='nowrap'>  </td>";
        strTable += "\n<td><align='center' class='cssTableView' style='width:100%;' nowrap='nowrap'>  </td>";
        strTable += "\n<td><align='center' class='cssTableView' style='width: 100%;  height: 40px; font-size: large; color:White; background-color:#000099;' nowrap='wrap'>BKI_EL - BÁO CÁO DANH SÁCH NHÂN SỰ" + "</td>";
        strTable += "\n</tr>";
        
        strTable += "\n</table>";

        //table noi dung
        strTable += "<table cellpadding='2' cellspacing='0' class='cssTableReport'>";
        strTable += "\n<tr>";
        strTable += "\n<td style='width:12%;' class='cssTableView' nowrap='nowrap'>STT</td>";
        strTable += "\n<td style='width:12%;' class='cssTableView' nowrap='nowrap'>Mã đối tượng</td>";
        strTable += "\n<td style='width:12%;' class='cssTableView' nowrap='nowrap'>Họ và Tên</td>";
        strTable += "\n<td style='width:12%;' class='cssTableView' nowrap='nowrap'>Ngày sinh</td>";
        strTable += "\n<td style='width:12%;' class='cssTableView' nowrap='nowrap'>Giới tính</td>";
        strTable += "\n<td style='width:12%;' class='cssTableView' nowrap='nowrap'>Thành viên</td>";
        strTable += "\n<td style='width:12%;' class='cssTableView' nowrap='nowrap'>Giảng viên</td>";
        strTable += "\n<td style='width:12%;' class='cssTableView' nowrap='nowrap'>Học viên</td>";
        strTable += "\n<td style='width:12%;' class='cssTableView' nowrap='nowrap'>Quê quán</td>";
        strTable += "\n<td style='width:12%;' class='cssTableView' nowrap='nowrap'>Địa chỉ hiện tại</td>";
        strTable += "\n<td style='width:12%;' class='cssTableView' nowrap='nowrap'>Điện thoại</td>";
        strTable += "\n<td style='width:12%;' class='cssTableView' nowrap='nowrap'>Số chứng minh</td>";
        strTable += "\n<td style='width:12%;' class='cssTableView' nowrap='nowrap'>Ngày cấp</td>";
        strTable += "\n<td style='width:12%;' class='cssTableView' nowrap='nowrap'>Nơi cấp</td>";
        strTable += "\n<td style='width:12%;' class='cssTableView' nowrap='nowrap'>Email cá nhân</td>";
        strTable += "\n<td style='width:12%;' class='cssTableView' nowrap='nowrap'>Email BKIndex</td>";
        strTable += "\n<td style='width:12%;' class='cssTableView' nowrap='nowrap'>Số tài khoản</td>";
        strTable += "\n<td style='width:12%;' class='cssTableView' nowrap='nowrap'>Tên ngân hàng</td>";
        strTable += "\n<td style='width:12%;' class='cssTableView' nowrap='nowrap'>Khoa ngành</td>";
        strTable += "\n<td style='width:12%;' class='cssTableView' nowrap='nowrap'>Trường đào tạo</td>";
        strTable += "\n<td style='width:12%;' class='cssTableView' nowrap='nowrap'>Giới thiệu</td>";        
        strTable += "\n</tr>";
        //loadGridExprort(ref strTable);
        loadDSExprort(ref strTable);
        strTable += "\n</table>";
    }

    private string loadExport()
    {
        try
        {
            string strHTML = "<html xmlns:o='urn:schemas-microsoft-com:office:office'"
            + "\n xmlns:x='urn:schemas-microsoft-com:office:excel'"
            + "\n xmlns='http://www.w3.org/TR/REC-html40'>"
            + "\n <head>"
            + "\n <meta http-equiv=Content-Type content='text/html; charset=utf-8'>"
            + "\n <meta name=ProgId content=Excel.Sheet>"
            + "\n <meta name=Generator content='Microsoft Excel 11'>"
            + "\n <link rel=File-List href='Book1_files/filelist.xml'>"
            + "\n <style id='Book1_28091_Styles'><!--table"
            + "\n 	{mso-displayed-decimal-separator:'\\.';"
            + "\n 	mso-displayed-thousand-separator:'\\,';}"
            + ".cssTitleReport"
            + "{font-family: tahoma; font-size: 11px;font-weight:normal;border: 1px #000000 solid;text-align:left;}"
            + ".cssTableView"
            + "{color:#FFFFFF;background-color:#800000;font-family: tahoma,Arial,Times New Roman; font-size: 12px;font-weight:bold;border: 1px #000000 solid;}"
            + "\n 	--></style>"
            + "\n 	</head>"
            + "\n 	<body><div id='Book1_28091' align=center x:publishsource='Excel'>";
            string strTable = "";
            loadTieuDe(ref strTable);
            strHTML += strTable;
            strHTML += "\n </div></body> ";
            strHTML += "\n </html> ";

            return strHTML;
        }
        catch
        {
            return "";
        }
    }
    #endregion

    
    #region Events
    protected void m_btt_locdulieu_Click(object sender, EventArgs e)
    {
        try
        {
            load_data_2_grv_doi_tuong();
            m_btt_xuatexcel.Enabled = true;
        }
        catch (Exception v_e)
        {
            m_lbl_thong_bao.Text = v_e.Message;
            m_lbl_thong_bao.Visible = true;
        }
    }

    protected void m_btt_xoatrang_Click(object sender, EventArgs e)
    {
        XoaTrang();
    }

    protected void m_btt_xuatexcel_Click(object sender, EventArgs e)
    {
        try
        {
            string html = loadExport();
            string strNamFile = "DSNhanSu" + ((m_cbo_cuong_vi.SelectedValue == "ALL") ? "" : m_cbo_cuong_vi.Text).ToString() + DateTime.Now.Day + "-" + DateTime.Now.Month + "-" + DateTime.Now.Year + ".xls";
            Response.Cache.SetExpires(DateTime.Now.AddSeconds(1));
            Response.Clear();
            Response.AppendHeader("content-disposition", "attachment;filename=" + strNamFile);
            Response.Charset = "UTF-8";
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.ContentType = "text/csv";
            Response.ContentType = "application/vnd.ms-excel";
            this.EnableViewState = false;
            Response.Write("\r\n");
            Response.Write(html);
            // Response.End();
            HttpContext.Current.ApplicationInstance.CompleteRequest();
        }
        catch (Exception v_e)
        {

            CSystemLog_301.ExceptionHandle(this, v_e);
        }
    }

    #endregion
}